package com.zonda.study.opengl.image;

import android.opengl.GLES20;

/**
 * Created by zhuqiuzhu on 17/3/24.
 * 台标
 */

public class StationLogo {

    // 位置分量总数（x和y或者是s和t）
    private static final int POSITION_COMPONENT_COUNT = 2;

    // 纹理分量总数
    private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2;

    //跨距
    private static final int STRIDE = (POSITION_COMPONENT_COUNT
            + TEXTURE_COORDINATES_COMPONENT_COUNT) * Constants.BYTES_PER_FLOAT;

    //纹理顶点信息，含：OpenGL 映射坐标和纹理的 UV 坐标信息
    private static final float[] VERTEX_DATA = {
            //分别代表：x,y,s,t
            0f, 0f, 0.5f, 0.5f,
            -1f, -1f, 0f, 1f,
            1f, -1f, 1f, 1f,
            1f, 1f, 1f, 0f,
            -1f, 1f, 0f, 0f,
            -1f, -1f, 0f, 1f
    };

    /*private static final float[] VERTEX_DATA = {
            // Order of coordinates: X, Y, S, T

            // Triangle Fan
            0f,    0f, 0.5f, 0.5f,
            -0.5f, -0.5f,   0f, 1f,
            0.5f, -0.5f,   1f, 1f,
            0.5f,  0.5f,   1f, 0f,
            -0.5f,  0.5f,   0f, 0f,
            -0.5f, -0.5f,   0f, 1f
    };*/

    private final VertexArray mVertexArray;

    public StationLogo() {
        mVertexArray = new VertexArray(VERTEX_DATA);
    }

    /**
     * 将顶点数组数据和着色器程序绑定起来
     *
     * @param program 着色器程序
     */
    public void bindData(ShaderProgram program) {

        mVertexArray.setVertexAttributePointer(
                0,
                program.getPositionAttributeLocation(),
                POSITION_COMPONENT_COUNT,
                STRIDE
        );

        mVertexArray.setVertexAttributePointer(
                POSITION_COMPONENT_COUNT,
                program.getTextureCoordinatesAttributeLocation(),
                TEXTURE_COORDINATES_COMPONENT_COUNT,
                STRIDE
        );
    }

    public void draw() {
        //绘制一个由六个顶点组成的三角形扇
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 6);
    }
}
